//
//  EditNameViewController.m
//  QT
//
//  Created by 甘世清 on 2025/1/8.
//

#import "EditNameViewController.h"

@interface EditNameViewController ()
@property (nonatomic , strong) FSTextView * nameTextField;

@end

@implementation EditNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"昵称");
    // Do any additional setup after loading the view.
    WS(weakSelf);
    [self zx_setRightBtnWithText:SQNSLocalString(@"提交") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [weakSelf SQ_Tijiao];
    }];
}
- (void)SQ_UIViewForSelfView{
    UIView * nameBG = [[UIView alloc]init];
    nameBG.layer.cornerRadius = 24;
    nameBG.layer.masksToBounds = YES;
    nameBG.layer.borderWidth = 0.5;
    nameBG.layer.borderColor = SQHexColor(@"#000000", 0.2).CGColor;
    [self.view addSubview:nameBG];
    [nameBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(42);
        make.width.mas_equalTo(300);
        make.height.mas_equalTo(50);
    }];
    self.nameTextField.text = [UserModel share].name;
    [self.view addSubview:self.nameTextField];
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.zx_navBar.mas_bottom).offset(50);
            make.width.mas_equalTo(300);
            make.height.mas_equalTo(50);
    }];
}
-(void)SQ_Tijiao{
    if (![self.nameTextField.formatText isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"昵称不能为空")];
        return;
    }
    WS(weakSelf);
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.nameTextField.formatText forKey:@"name"];
    [HttpTool post:@"user/config" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel  * model = [UserModel share];
        model.name = self.nameTextField.formatText;
        [UserModel updateLocalUserModel:model];
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"修改成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf SQ_popViewVC];
        });
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
       [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (FSTextView *)nameTextField{
    if (!_nameTextField) {
        _nameTextField = [[FSTextView alloc]init];
        _nameTextField.textAlignment = NSTextAlignmentCenter;
        _nameTextField.font = FONT_MEDIUM(16);
        _nameTextField.textColor = SQColor(@"#000000");
        _nameTextField.backgroundColor = UIColor.clearColor;
        _nameTextField.returnKeyType = UIReturnKeyDone;
        _nameTextField.maxLength = 15;
    }
    return _nameTextField;
}
 

@end
